<template>
    <div>
      <cfg-table
        ref="table"
        :columns="columns"
        :data="data"
        :loading="loading"
        :config="config"
        :pageSizeOpts="pageSizeOpts"
        :pageNavigation="pageNavigation"
        :queryFormItem="queryFormItem"
        :collapseFormItem="collapseFormItem"
        :additionalAction="additionalAction"
        @loadPage="loadPage"
        @handleCreate="handleCreate"
        @handleDetail="handleDetail"
        @handleEdit="handleEdit"
        @handleDelete="handleDelete"
        @handleDeleteMultiple="handleDeleteMultiple"
        @handleSearch="handleSearch"
        @handleReset="handleReset"
      ></cfg-table>
      <warn-new-add-modal
        v-bind="addNewPanel"
        ref="addModal"
        :modelType="modelType"
        :columns="formColumns"
        :rules="rules"
        :energyList="energyList"
        @submit="handleNewAddSubmit"
      ></warn-new-add-modal>
      <warn-edit-modal
        v-bind="editPanel"
        :modelType="modelType"
        :columns="formColumns"
        :rules="rules"
        :energyList="energyList"
        @submit="handleEditSubmit"
      ></warn-edit-modal>
      <read-modal
        v-bind="readPanel"
        :modelType="modelType"
        :columns="columns"
        :rules="rules"
      ></read-modal>
    </div>
</template>

  <script>
    import warnNewAddModal from './components/warnNewAddModal.vue';
    import warnEditModal from './components/warnEditModal.vue';
    import readModal from '@/pages/components/readModal.vue';
    import { typeClassName } from '@/pages/config/classNameMap';
    import mixin from '@/pages/components/mixins/mixin'
    import bindAccount from '@/pages/components/bindAccount';
    import columnsMixin from './config'
    export default {
        components: { warnNewAddModal,warnEditModal,readModal, bindAccount },
        name: 'energyKPI',
        mixins: [mixin, columnsMixin],
        data () {
            return {
                classObject: null, // 单个实例
                collectionObject: null, // 所有实例
                className: typeClassName['energyKPI'].className,
                type: typeClassName['energyKPI'].type,
                modelType: typeClassName['energyKPI'].modelType,
                energyStatus: [], // 能源状态
                energyStatusClassObject: null, // 状态模型
                energyClassObject: null, // 能源实例
                selectionData: [], // 绑定预警负责人
                isSingleCheck: false,
                addNewPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2
                },
                editPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                readPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                accountPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                rules: {
                    sf_kpiName: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
                    sf_energyName: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
                    sf_status: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ]
                },
                loading: false,
                config: {
                    search: true,
                    addButton: true,
                    batchDeleteButton: true,
                    tableSizeIcon: true,
                    fullscreenIcon: true,
                    refreshIcon: false,
                    columnSettingIcon: true,
                    action: true,
                    additionalAction: false
                },
                pageSizeOpts: [10, 30, 50, 100],
                pageNavigation: { total: 0, pageSize: 10, pageNumber: 1 },
                energyCodeOptions:[],
                energyList:[],
                scaleOptions:[
                  {label:'时',value:'时'},
                  {label:'分',value:'分'},
                ],
                sourceOptions: [{label: '外采', value: 'out'}, {label: '自产', value: 'product'}],
            };
        },
        computed: {
            additionalAction () {
                let additionalAction = {
                    key: 'additionalAction',
                    title: {
                        'zh-CN': '附加操作',
                        'en-US': 'additional action'
                    }[this.G_Locale],
                    slot: 'additionalAction',
                    width: 200,
                    fixed: 'right',
                    align: 'center',
                    show: true,
                    buttons: [
                    ]
                }
                this.G_IsMobile && (additionalAction.width = 90)
                return additionalAction;
            },
            data () {
                if (!!this.collectionObject) {
                    return this.collectionObject.records.map(d => {
                        return {
                            ...d,
                            ...d.values
                        }
                    })
                } else {
                    return [];
                }
            }
        },
        beforeMount () {
            this.collectionObject = this.$modelObject.create(`${this.modelType}Collection`);
            this.classObject = this.$modelObject.create(this.modelType);
            this.energyClassObject = this.$modelObject.create(`${typeClassName['energyInfo'].modelType}`);
            this.energyStatusClassObject = this.$modelObject.create(typeClassName['paramStatus'].modelType);
            this.setOptions('paramStatus', this.energyStatusClassObject, 'energyStatus')
            this.loadPage(this.pageNavigation, null);
            // 能源名称
            let energyInfoFilter = {
                className: typeClassName['energyInfo'].className,
                type: typeClassName['energyInfo'].type
            }
            this.getEnergyCodeOptions(energyInfoFilter)
        },
        mounted () {},
        methods: {
            getEnergyCodeOptions(condition){
               this.$api.TemplateInstance.query(condition).then(res=>{
                  if(!res){
                     this.energyCodeOptions = [];
                     this.energyList = [];
                     return;
                  }
                  this.energyList = res.map(d=>{return {id:d.id,...d.values}})
                  this.energyCodeOptions = res.map(d=>({label:d.values.sf_energyName,value:d.values.sf_energyCode}))
               })
            },
        }
    };
  </script>
  <style scoped lang="scss"></style>
